from puter_code.forms import RegisterForm
from django.shortcuts import render
from django.views.generic import View
from captcha.models import CaptchaStore
from captcha.helpers import  captcha_image_url
from django.http import HttpResponse
import json
from django.http import JsonResponse




# Create your views here.
def index(request):
    register_form = RegisterForm()
    return render(request,'图文验证.html',{'register_form': register_form})

# 验证和刷新
def img_refresh(request):
    if not request.is_ajax():
        return HttpResponse('不是ajax请求')
    new_key = CaptchaStore.generate_key()
    to_json_response = {
        'hashkey': new_key,
        'imge_url': captcha_image_url(new_key)
    }
    return HttpResponse(json.dumps(to_json_response))
# 验证
def img_check(request):
    print("验证用户输入的图片验证码...")
    if request.is_ajax():
        cs = CaptchaStore.objects.filter(
            response=request.GET.get('code'),
            hashkey = request.GET.get('hashkey'))
        print(cs)
        if cs:
            json_data = {'status':1}
        else:
            json_data={'status':0}
        return JsonResponse(json_data)
    else:
        json_data = {'statis':0}
        return JsonResponse(json_data)













